package com.aviation.sell_ticket.controller;

import cn.hutool.core.lang.Validator;
import com.aviation.sell_ticket.pojo.Aviation_passenger;
import com.aviation.sell_ticket.service.PassengerService;
import com.aviation.sell_ticket.tool.IdCardUtil;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("PassengerAction")
public class PassengerController {

    @Resource
    private PassengerService passengerService;

    @PostMapping("passengerList")
    public Map<String,Object> passengerList(HttpServletRequest request){
        return passengerService.passengerList(String.valueOf(request.getAttribute("userId")));
    }

    @PostMapping("addPassenger")
    public Map<String,Object> addPassenger(Aviation_passenger passenger,HttpServletRequest request){

        if(Validator.isNull(passenger.getPassenger_name()) || passenger.getPassenger_name().equals("")){
            return ResultUtil.success(ResultEnum.NAME_ERROR, null);
        }else if(Validator.isNull(passenger.getPassenger_number()) || !IdCardUtil.isIdcard(passenger.getPassenger_number())){
            return ResultUtil.success(ResultEnum.ID_CARD_ERROR, null);
        }

        if(!Validator.isNull(passenger.getPassenger_phone()) && !passenger.getPassenger_phone().equals("")){
            if(!Validator.isMobile(passenger.getPassenger_phone())){
                return ResultUtil.success(ResultEnum.THEMOBILE_PHONE_NUMBER_IS_INCORRECT, null);
            }
        }
        return passengerService.addPassenger(passenger,String.valueOf(request.getAttribute("userId")));
    }

    @PostMapping("delPassenger")
    public Map<String,Object> delPassenger(String passenger_id,HttpServletRequest request){
        if(Validator.isNull(passenger_id) || passenger_id.equals("")){
            return ResultUtil.success(ResultEnum.FAILED_TO_DELETE_PASSENGER_INFORMATION, null);
        }
        return passengerService.delPassenger(passenger_id,String.valueOf(request.getAttribute("userId")));
    }

}
